package com.wt.chat.controller;

import com.wt.chat.context.ContextHolder;
import com.wt.chat.entity.Dialog;
import com.wt.chat.model.DialogModel;
import com.wt.chat.query.PageQuery;
import com.wt.chat.response.ResponseResult;
import com.wt.chat.service.DialogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description
 * @author: wangtao
 * @date:13:36 2019/11/12
 * @email:taow02@jumei.com
 */
@Controller
@RequestMapping("/dialog")
public class DialogController {

	@Autowired
	private DialogService dialogService;

	/**
	 * 查询当前会话，也就是还未关闭的会话
	 */
	@RequestMapping("/current")
	public ResponseResult current() {
		Integer kefuId = ContextHolder.currentKefuId();
		List<DialogModel> dialogs = dialogService.currentDialog(new Dialog().setKefuId(kefuId).setStatus(Dialog.DialogStatus.OPEN.getValue()));
		return new ResponseResult().code(0).body(dialogs);
	}

	/**
	 * 查询历史会话，也就是已经关闭的会话
	 */
	@RequestMapping("/history")
	public ResponseResult history() {
		Integer kefuId = ContextHolder.currentKefuId();
		List<DialogModel> dialogs = dialogService.historyDialog(new Dialog().setKefuId(kefuId).setStatus(Dialog.DialogStatus.CLOSE.getValue()), new PageQuery(1,10));
		return new ResponseResult().code(0).body(dialogs);
	}

	/**
	 * 根据会话id查询回话
	 */
	@RequestMapping("/getById/{dialogId}")
	public ResponseResult getById(@PathVariable("dialogId") Integer dialogId) {
		List<DialogModel> dialogs = dialogService.currentDialog(new Dialog().setId(dialogId));
		return new ResponseResult().code(0).body(dialogs);
	}

	/**
	 * 关闭会话
	 */
	@RequestMapping("/close/{dialogId}")
	public ResponseResult close(@PathVariable("dialogId") Integer dialogId) {
		dialogService.close(new Dialog().setId(dialogId).setWhoClose(Dialog.WhoClose.KEFU.getValue()));
		return new ResponseResult().code(0);
	}


}
